package com.guodu.dao.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialGhycDao;
import com.guodu.entity.MaterialGhyc;
import com.guodu.entity.MaterialghycStatus;
import com.guodu.entity.Units;
import com.guodu.vo.GhycVo;
import com.guodu.vo.MaterialGhycQueryVo;

@Repository("materialGhycDao")
public class MaterialGhycDaoImpl extends BaseDao implements MaterialGhycDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialGhyc> findByVo(MaterialGhycQueryVo materialGhycQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialGhyc m where 1=1");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" order by ghycId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public int findSizeByVo(
			MaterialGhycQueryVo materialGhycQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from MaterialGhyc m where 1=1");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		return super.findInt(hql.toString());
	}

	@Override
	public MaterialGhyc findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialGhyc) super.findById(MaterialGhyc.class, id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialGhyc.class, id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		super.deleteAllById(MaterialGhyc.class, ids);
	}

	@Override
	public void save(MaterialGhyc entity) {
		// TODO Auto-generated method stub
		entity.setGhycId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		super.save(entity);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialGhyc set status =");
		hql.append(status);
		hql.append(" where ghycId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void update(MaterialGhyc entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@Override
	public void changeStatus(List<MaterialghycStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialghycStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialghycStatus.class, "materialId", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialGhyc> findAllByVo(
			MaterialGhycQueryVo materialGhycQueryVo, Units units) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialGhyc m where 1=1");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=2");
		}
		hql.append(" order by createTime desc");
		return super.find(hql.toString());
	}

	@Override
	public GhycVo findAllMoney(MaterialGhycQueryVo materialGhycQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(cgMoney),sum(xhMoney) from MaterialGhyc m where 1=1");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		List<?> list = super.find(hql.toString());
		GhycVo ghycVo = new GhycVo();
		if(list.size()>0){
			Object[] os  = (Object[]) list.get(0);
			if(os[0]!=null||os[1]!=null){
			ghycVo.setAllcg(os[0].toString());
			ghycVo.setAllxh(os[1].toString());
			}
		}
		return ghycVo;
	}

	@Override
	public GhycVo findAllKcMoney(MaterialGhycQueryVo materialGhycQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(beginMoney),sum(endMoney) from MaterialGhyc m where 1=1");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		List<?> list = super.find(hql.toString());
		GhycVo ghycVo = new GhycVo();
		if(list.size()>0){
			Object[] os  = (Object[]) list.get(0);
			if(os[0]!=null||os[1]!=null){
			ghycVo.setAllqckc(os[0].toString());
			ghycVo.setAllqmkc(os[1].toString());
			}
		}
		return ghycVo;
	}

	@Override
	public boolean examinAll(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialGhyc m set status=1  where status=0");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+units.getUnitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean huituiAll(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialGhyc m set status=2  where status=0");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+units.getUnitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean shanchuAll(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		
		StringBuffer hql = new StringBuffer("delete from  MaterialGhyc m  where (status=0 or status=2)");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}else{
			hql.append(" and unitsId="+units.getUnitsId());
		}
		super.executeUpdate(hql.toString());
		return true;
	}


	@Override
	public boolean allBack(MaterialGhycQueryVo materialGhycQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialGhyc m set status=0  where status=1");
		if(materialGhycQueryVo!=null){
			if(materialGhycQueryVo.getMaterialType()!=null&&materialGhycQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialGhycQueryVo.getMaterialType()+"'");
			}
			if(materialGhycQueryVo.getDrawingnum()!=null&&materialGhycQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialGhycQueryVo.getDrawingnum()+"%'");
			}
			if(materialGhycQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialGhycQueryVo.getFunitsId()+")");
			}
			if(materialGhycQueryVo.getMaterialCode()!=null&&materialGhycQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialGhycQueryVo.getMaterialCode()+"%'");
			}
			if(materialGhycQueryVo.getMaterialName()!=null&&materialGhycQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialGhycQueryVo.getMaterialName()+"%'");
			}
			if(materialGhycQueryVo.getGhycBeginMonth()!=null&&materialGhycQueryVo.getGhycBeginMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialGhycQueryVo.getGhycBeginMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getGhycEndMonth()!=null&&materialGhycQueryVo.getGhycEndMonth().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialGhycQueryVo.getGhycEndMonth()+"','yyyy-mm')");
			}
			if(materialGhycQueryVo.getSpecifications()!=null&&materialGhycQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialGhycQueryVo.getSpecifications()+"%'");
			}
			if(materialGhycQueryVo.getTexture()!=null&&materialGhycQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialGhycQueryVo.getTexture()+"%'");
			}
			if(materialGhycQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialGhycQueryVo.getUnitsId());
			}
			if(materialGhycQueryVo.getCreateBeginTime()!=null&&materialGhycQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialGhycQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getCreateEndTime()!=null&&materialGhycQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialGhycQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialGhycQueryVo.getStatus()!=null&&materialGhycQueryVo.getStatus().trim().length()>0&&!materialGhycQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialGhycQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		
		super.executeUpdate(hql.toString());
		return true;
	}

	
	


}
